﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Spring.Context;
using System.Collections;
using Spring.Objects.Factory;
namespace BLL
{
    public class SpringDependencyResolver : IDependencyResolver
    {
        IApplicationContext _container;

        public SpringDependencyResolver(IApplicationContext container)
        {
            _container = container;
        }

        public object GetService(Type serviceType)
        {
            try
            {
                IEnumerator it = GetServices(serviceType).GetEnumerator();
                if (it.MoveNext())
                {
                    return it.Current;
                }
                return null;
            }
            catch (Exception)
            {
                throw new ObjectCreationException(string.Format("no services of type '{0}' defined", serviceType.FullName));
            }

        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            foreach (object o in _container.GetObjectsOfType(serviceType).Values)
            {
                yield return o;
            }
        }
    }
}
